//封装分类轮播图相关业务逻辑代码
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { onBeforeRouteUpdate } from "vue-router";
import { getTopCategoryAPI } from "@/apis/category";
export function useCategory() {
        const route = useRoute();
        //获取分类数据
        const categoryData = ref({});
        const getCategory = async (id = route.params.id) => {
                // 如何在setup中获取路由参数 useRoute() -> route 等价于this.$route
                const res = await getTopCategoryAPI(id);
                categoryData.value = res.result;
        };
        onMounted(() => {
                getCategory(route.params.id)
        })
        //路由参数变化的时候，可以把分类接口数据重新发送
        onBeforeRouteUpdate((to) => {
                console.log("路由变化了");
                // console.log(to, "to");
                //存在问题：使用最新的路由参数请求最新的分类数据
                getCategory(to.params.id);
        });

        return { categoryData,getCategory}
}
